# -*- coding: UTF-8 -*-

import requests
from bs4 import BeautifulSoup

'''
基金经理
基金经理网址：http://fundf10.eastmoney.com/jjjl_0062279.html
根据基金的代码获取基金经理的基本信息：
getfundmanagerinfo - 基金的经理列表
getmanagefundlist - 基金经理的信息列表


'''

class HTMLFundManager(object):

    def __init__(self, code_fund):

        self.code_fund = code_fund
        self.fm_url = 'http://fundf10.eastmoney.com/jjjl_' + code_fund + '.html'
        self.fm_html = requests.get(self.fm_url)    # 返回一个对象
        self.fm_bs = BeautifulSoup(self.fm_html.text, "html.parser")

        # print("fm_bs:")
        # print(self.fm_bs)
        # self.fbp_tree = etree.HTML(self.fm_html.content.decode('utf-8'))
        # self.fbp_tree = etree.HTML(self.fm_html.text)

    # 获取基金对应的基金经理列表
    def getfundmanagerinfo(self):

        ls_fundmanagerinfo = []
        em_table = self.fm_bs.find("table", "w782 comm jloff")
        em_ths = em_table.thead.tr.find_all("th")
        em_trs = em_table.tbody.find_all("tr")
        for em_tr in em_trs:
            em_tds = em_tr.find_all("td")
            dc_fundmanagerinfo = {}
            for i in range(0, len(em_ths)):
                if i == 2:
                    em_as = em_tds[i].find_all('a')
                    a_s = []
                    for em_a in em_as:
                        a_s.append(em_a.string)
                    dc_fundmanagerinfo[em_ths[i].string] = a_s
                else:
                    dc_fundmanagerinfo[em_ths[i].string] = em_tds[i].string

            ls_fundmanagerinfo.append(dc_fundmanagerinfo)

        return ls_fundmanagerinfo

    # 获取当前还在任职的基金列表
    def getmanagefundlist(self):

        dc_manage = {}
        em_tables = (self.fm_bs.find_all("table", "w782 comm jloff"))
        for j in range(1, len(em_tables)):
            name = em_tables[j].previous_sibling.label.a.string
            ths = em_tables[j].thead.tr.find_all('th')
            trs = em_tables[j].tbody.find_all('tr')
            ls_managefund = []
            for tr in trs:
                tds = tr.find_all('td')
                if tds[4].string == '至今':
                    dc_managefund = {}
                    for i in range(0, len(tds)):
                        if i == 0 or i == 1:
                            dc_managefund[ths[i].string] = tds[i].a.string
                        else:
                            dc_managefund[ths[i].string] = tds[i].string
                    ls_managefund.append(dc_managefund)

                else:
                    continue
            dc_manage[name] = ls_managefund

        return dc_manage

if __name__ == '__main__':
    # fm = HTMLFundManager('006279')
    fm = HTMLFundManager('519760')
    # fm.getfundmanagerinfo()
    print('\n')
    dc = fm.getmanagefundlist()

    for k, v in dc.items():
        for dcstr in v:
            if dcstr['基金代码'] == '519760':
                print(dcstr['基金代码'], dcstr['基金名称'], k, dcstr['起始时间'], dcstr['任职天数'], '\n')
        # print(k, v)
    # ls_fundmanagerinfo = fm.getfundmanagerinfo()
    #
    # for dc_fundmanagerinfo in ls_fundmanagerinfo:
    #     for k, v in dc_fundmanagerinfo.items():
    #         print(k, v)

        # print('\n')
